function M = connect(ne,bcL,bcR)
% function M = connect(ne,bcL,bcR)
%     This function constructs the connectivity matrix
%     relating J+ and J- for a sequence of ne slabs. 
%     The boundary conditions possible are:
%      bcL (bcR) = 0 --> zero re-entrant current at left (right)
%      bcL (bcR) = 1 --> reflective at left (right)
%      bcL       = 2 --> periodic

M = zeros( 2*ne, 2*ne );

for i = 1:(ne-1)
    M(2*i+1,2*i) = 1.0;
    M(2*i,2*i+1) = 1.0;
end

if bcL == 1
    M(1,1) = 1;
elseif bcL == 2
    M(end,1) = 1;
    M(1,end) = 1;
    return
end

if bcR == 1
    M(end,end) = 1;
end